package com.ruoyi.info.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.health.domain.Concern;
import com.ruoyi.health.service.IConcernService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 关注-->养老资源 Controller
 *
 * @author libin
 * @date 2023-08-15
 */
@RestController
@RequestMapping("/info/healthConcern")
public class FrontConcernController extends BaseController {

    @Autowired
    private IConcernService concernService;


    /**
     * 新增[关注]   客户点关注
     */
    @Log(title = "新增关注养老资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Concern concern)
    {
        //【注意，这里要获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId)){
            concern.setUserId(frontUserId);
            // TODO:不存在的养老资源id，不能关注
            return toAjax(concernService.insertConcern(concern));
        }else{
            return AjaxResult.error("关注失败！无法获取前台用户信息！");
        }

    }

    /**
     * 删除[关注]
     */
    @Log(title = "取消关注养老资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{resourceId}")
    public AjaxResult remove(@PathVariable Integer resourceId){
        //【注意，这里要获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId) && StringUtils.isNotNull(resourceId)){
            //【frontUserId】用户取消对【resourceId】资源的关注
            Concern concern = new Concern();
            concern.setUserId(frontUserId);
            concern.setResourceId(resourceId);
            return toAjax(concernService.deleteConcern(concern));
        }else{
            return AjaxResult.error("取消关注失败！无法获取前台用户信息！");
        }

    }


    /**
     * [关注]列表
     */
    @GetMapping("/list")
    public TableDataInfo listByUserId() {
        //【注意，这里要获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId)){
            return getDataTable(concernService.selectConcernWithResourceByUserId(frontUserId));
        }else{
            return getDataTable(null);

        }

    }


    @GetMapping("/{resourceId}")
    public AjaxResult getConcernStatus(@PathVariable Integer resourceId) {
        //【注意，这里要获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        System.out.println(frontUserId);
        if(StringUtils.isNotNull(frontUserId)){
            Concern concern = new Concern();
            concern.setUserId(frontUserId);
            concern.setResourceId(resourceId);
            //查询是否关注了此 concern
            int concernStatus = concernService.getConcernStatus(concern);
            HashMap<String, Integer> map = new HashMap<>();
            map.put(Constants.STATUS,concernStatus);
            //返回结果
            return AjaxResult.success(map);
        }else{
            return AjaxResult.error(HttpStatus.GET_DATA_ERROR,"获取关注信息失败！无法获取前台用户信息！");
        }

    }
}
